/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-05-29 17:06:35
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-07-16 14:32:40
 * @FilePath: \vite-project\src\hooks\useInspection.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createStuff, save, updateStuff } from '@/apis/basicSystem/inspection';
import { App } from 'antd';
import { useState } from 'react';
import { useParams } from 'react-router-dom-v5-compat';

export function useInspection() {
    const { id } = useParams();
    const [priceData, setPriceData] = useState();
    const [basicData, setBasicData] = useState();
    const [result, setResult] = useState();
    const [loading, setLoading] = useState(false);
    const { message } = App.useApp();

    const formatData = (fdata: any) => {
        return {
            ...fdata,
            orderMaterialId: fdata?.orderMaterial?.id,
            plate: fdata?.orderMaterial?.plate,
            categoryId: fdata?.orderMaterial?.categoryId,
            categoryName: fdata?.orderMaterial?.categoryName,
            orderMaterial: void 0,

            basicCheckList: fdata?.basicCheckList?.map((item: any) => {
                Reflect.deleteProperty(item, 'imageUrls');
                item.images = item.images?.map((image: any) => image.filePath);
                return item;
            }),
            otherCheckList: fdata?.otherCheckList?.map((item: any) => {
                Reflect.deleteProperty(item, 'imageUrls');
                item.images = item.images?.map((image: any) => image.filePath);
                return item;
            }),
            increaseCheckList: fdata.increaseCheckList || [],
            decreaseCheckList:
                fdata?.decreaseCheckList?.map((item: any) => {
                    Reflect.deleteProperty(item, 'imageUrls');
                    item.images = item.images?.map((image: any) => image.filePath);
                    return item;
                }) || [],
        };
    };

    const combineNewAndOldData = (oldData, newData) => {
        const oldIds = oldData?.map((item) => item?.id) || [];
        const newIds = newData?.map((item) => item?.id) || [];
        const res = [];
        let index = -1;
        for (let i = 0; i < newIds.length; i++) {
            index = oldIds.indexOf?.(newIds[i]);
            if (index >= 0) {
                res.push(oldData?.[index]);
            } else {
                res.push(newData?.[i]);
            }
        }
        return res;
    };

    // 存草稿
    const onSubmitStuff = (fdata: any) => {
        setLoading(true);
        const curFdata = formatData(fdata);
        if (id === 'add' && !result?.id) {
            createStuff(curFdata)
                .then((res) => {
                    message.success('创建草稿成功');
                    if (res?.id) {
                        setResult(res);
                    } else {
                        history.back();
                    }
                })
                .finally(() => {
                    setLoading(false);
                });
        } else if (id) {
            updateStuff({
                id: result?.id || id,
                ...curFdata,
            })
                .then((res) => {
                    message.success('修改草稿成功');
                })
                .finally(() => {
                    setLoading(false);
                });
        }
    };

    const onSubmit = (fdata: any, successCallback: (arg0: unknown) => void) => {
        const curFdata = formatData(fdata);
        setLoading(true);
        save({
            ...(id === 'add' && !result?.id ? {} : { id: result?.id || id }),
            ...curFdata,
        })
            .then((res) => {
                setResult(res);
                successCallback?.(res);
            })
            .finally(() => {
                setLoading(false);
            });
    };

    return {
        loading,
        result,
        basicData,
        setBasicData,
        priceData,
        setPriceData,
        setResult,
        onSubmitStuff,
        onSubmit,
        combineNewAndOldData,
    };
}
